package com.iflytek.ci.cpms.dao.impl;

import java.util.ArrayList;
import java.util.List;

import com.iflytek.ci.cpms.dao.ProjectUserBatchDao;
import com.iflytek.ci.cums.dao.AccountDao;
import com.iflytek.ci.cums.domain.Account;
import com.iflytek.ci.cums.dto.ProjectAccount;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 * <p>
 *     项目批量导入用户信息 .
 * </p>
 *
 * @author phwang
 * @version 1.0 2012-12-10 上午9:08
 * @since JDK 1.5
 */
@Repository("ProjectUserBatchDao")
public class ProjectUserBatchDaoImpl implements ProjectUserBatchDao {

    private static final List<String> INCLUDE_ROLES = new ArrayList<String>() {{
        add("架构师");
        add("开发经理");
        add("开发组员");
        add("项目经理");
	}

		private static final long serialVersionUID = -8989510153862337759L;
	};
	/**
     * 注入批处理的SqlSessionTemplate
     */
    @Qualifier("batchSqlSession")
    @Autowired
    private SqlSessionTemplate batchSqlSession;

    /**
     * get uids
     *
     * @param accounts 用户信息
     */
    @Override
    public List<String> batch_account(List<ProjectAccount> accounts) {
        List<String> logins=new ArrayList<String>();
        SqlSession sqlSession = batchSqlSession.getSqlSessionFactory().openSession(ExecutorType.BATCH);
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
        for (ProjectAccount account : accounts) {
            if (INCLUDE_ROLES.contains(account.getRole())&&account.getLogin()!=null){
                logins.add(account.getLogin());
                //covert projectAccount to Account
               //accountDao.addUser(covert(account));
            }
        }
        sqlSession.flushStatements();
        sqlSession.commit();
        sqlSession.close();
        return logins;
    }

	//将每行对象转换成Account对象
	private Account covert(ProjectAccount account) {
		Account account0=new Account();
        if(account.getLogin()!=null)
        {
            account0.setLogin(account.getLogin());
            account0.setName(account.getName());
            account0.setMail(account.getEmail());
            account0.setPassword(account.getPassword());
        }
        return account0;  //To change body of created methods use File | Settings | File Templates.
    }

    /**
     * get uids
     *
     * @param logins
     */
    @Override
    public long[] idsFromLogins(List<String> logins) {
        SqlSession sqlSession = batchSqlSession.getSqlSessionFactory().openSession(ExecutorType.BATCH);
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
        long[] uids = new long[logins.size()];
        for (int i = 0; i < logins.size(); i++) {
            uids[i] = accountDao.idFromLogin(logins.get(i));
        }
        sqlSession.flushStatements();
        sqlSession.commit();
        sqlSession.close();
        return uids;
    }


}
